package org.batatasframework.support.searchable.writer;

import java.util.List;

import org.batatasframework.support.database.spi.ResultWritable;
import org.batatasframework.support.searchable.ResultHeadInfo;
import org.batatasframework.support.searchable.SearchableInputStream;
import org.batatasframework.support.searchable.SearchableType;
import org.batatasframework.support.searchable.result.ResultRow;


public abstract class AbstractResultWriter implements ResultWritable{
	
	protected final SearchableInputStream inputStream;
	
	public AbstractResultWriter(SearchableInputStream inputStream) throws Exception{
		checkType(inputStream.getSearchableType());
		
		this.inputStream = inputStream;
	}
	
	@Override
	public final void flush() throws Exception {
		
		renderHead(inputStream.getHeads());
		
		//是否需要使用分页的显示，可以由这个子类来决定
		renderBody(inputStream.getRows());
	}
	
	public void renderBody(List<? extends ResultRow> rows) throws Exception{
		for(ResultRow row : rows){
			row.writeTo(this);
		}
	}
	
	/**
	 * TODO 这种方式估计得改一改。
	 * @param heads
	 */
	protected void renderHead(ResultHeadInfo[][] heads){ }
	
	/**
	 * <pre>
	 * 用于检查这种方式是否接受某一种查询的类型，需要由子类决定；
	 * 如：图形统计的，是不接受列表查询结果的
	 * </pre>
	 */
	protected void checkType(SearchableType type){ }

	@Override
	public void newLine(Object value) throws Exception {
		newLine();
		append(value);
	}
}
